home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59 / 59.xpi / chrome / useragentswitcher.jar / content / useragentswitcher / upgrade.js < prev    next >
Text File  |  2009-06-30  |  7KB  |  225 lines

  1. // User Agent Switcher upgrade
  2. var UserAgentSwitcherUpgrade =
  3. {
  4.     // Gets the version
  5.     getVersion: function()
  6.     {
  7.         return "0.7.2";
  8.     },
  9.  
  10.     // Installs the user agents
  11.     installUserAgents: function()
  12.     {
  13.         // If the user agent directory does not exist
  14.         if(!UserAgentSwitcherImporter.getUserAgentDirectoryLocation().exists())
  15.         {
  16.             UserAgentSwitcherImporter.createUserAgentDirectory();
  17.         }
  18.  
  19.         // If the user agent file does not exist
  20.         if(!UserAgentSwitcherImporter.getUserAgentFileLocation().exists())
  21.         {
  22.             UserAgentSwitcherImporter.createUserAgentFile();
  23.             UserAgentSwitcherImporter.reset();
  24.         }
  25.     },
  26.  
  27.     // Migrate to version 0.7
  28.     migrateTo07: function()
  29.     {
  30.         var description    = null;
  31.         var userAgent      = null;
  32.         var userAgentCount = UserAgentSwitcherPreferences.getIntegerPreference("useragentswitcher.user.agents.count");
  33.         var userAgentFile  = null;
  34.         var xmlDocument    = document.implementation.createDocument("", "", null);
  35.         var rootElement    = xmlDocument.createElement("useragentswitcher");
  36.     
  37.         // If the user agent directory does not exist
  38.         if(!UserAgentSwitcherImporter.getUserAgentDirectoryLocation().exists())
  39.         {
  40.             UserAgentSwitcherImporter.createUserAgentDirectory();
  41.         }
  42.         
  43.         // If the user agent file exists
  44.         if(UserAgentSwitcherImporter.getUserAgentFileLocation().exists())
  45.         {
  46.             userAgentFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  47.  
  48.             userAgentFile.initWithPath(UserAgentSwitcherImporter.getUserAgentFileLocation().path);
  49.         }
  50.         else
  51.         {
  52.             userAgentFile = UserAgentSwitcherImporter.createUserAgentFile();
  53.         }
  54.         
  55.         // Loop through the user agents
  56.         for(var i = 1; i <= userAgentCount; i++)
  57.         {
  58.             description = UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".description");
  59.             
  60.             // If the description is set
  61.         if(description)
  62.         {
  63.                 userAgent = xmlDocument.createElement("useragent");
  64.  
  65.                 userAgent.setAttribute("description", description);
  66.  
  67.                 // If the user agent is set
  68.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".useragent"))
  69.                 {
  70.                     userAgent.setAttribute("useragent", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".useragent"));
  71.                 }
  72.                 else
  73.                 {
  74.                     userAgent.setAttribute("useragent", "");
  75.                 }
  76.         
  77.                 // If the app code name is set
  78.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".appcodename"))
  79.                 {
  80.                     userAgent.setAttribute("appcodename", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".appcodename"));
  81.                 }
  82.                 else
  83.                 {
  84.                     userAgent.setAttribute("appcodename", "");
  85.                 }
  86.         
  87.                 // If the app name is set
  88.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".appname"))
  89.                 {
  90.                     userAgent.setAttribute("appname", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".appname"));
  91.                 }
  92.                 else
  93.                 {
  94.                     userAgent.setAttribute("appname", "");
  95.                 }
  96.         
  97.                 // If the app version is set
  98.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".appversion"))
  99.                 {
  100.                     userAgent.setAttribute("appversion", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".appversion"));
  101.                 }
  102.                 else
  103.                 {
  104.                     userAgent.setAttribute("appversion", "");
  105.                 }
  106.         
  107.                 // If the platform is set
  108.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".platform"))
  109.                 {
  110.                     userAgent.setAttribute("platform", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".platform"));
  111.                 }
  112.                 else
  113.                 {
  114.                     userAgent.setAttribute("platform", "");
  115.                 }
  116.         
  117.                 // If the vendor is set
  118.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".vendor"))
  119.                 {
  120.                     userAgent.setAttribute("vendor", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".vendor"));
  121.                 }
  122.                 else
  123.                 {
  124.                     userAgent.setAttribute("vendor", "");
  125.                 }
  126.         
  127.                 // If the vendor sub is set
  128.                 if(UserAgentSwitcherPreferences.isPreferenceSet("useragentswitcher." + i + ".vendorsub"))
  129.                 {
  130.                     userAgent.setAttribute("vendorsub", UserAgentSwitcherPreferences.getStringPreference("useragentswitcher." + i + ".vendorsub"));
  131.                 }
  132.                 else
  133.                 {
  134.                     userAgent.setAttribute("vendorsub", "");
  135.                 }
  136.             }        
  137.  
  138.             rootElement.appendChild(userAgent);
  139.             UserAgentSwitcherPreferences.deletePreferenceBranch("useragentswitcher." + i + ".");
  140.         }
  141.     
  142.         UserAgentSwitcherPreferences.deletePreference("useragentswitcher.user.agents.count");
  143.  
  144.         // If old user agents could be migrated
  145.         if(rootElement.childNodes.length > 0)
  146.         {
  147.             var outputStream  = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  148.             var xmlSerializer = new XMLSerializer();
  149.  
  150.             xmlDocument.appendChild(rootElement);
  151.     
  152.             outputStream.init(userAgentFile, 0x04 | 0x08 | 0x20, 00644, null);
  153.             xmlSerializer.serializeToStream(xmlDocument, outputStream, "utf-8");
  154.             outputStream.close();
  155.         } 
  156.         else
  157.         {
  158.             UserAgentSwitcherImporter.reset();
  159.         }
  160.     },
  161.  
  162.     // Opens the upgrade page
  163.     openUpgradePage: function()
  164.     {
  165.         var windowContent = window.getBrowser();
  166.     
  167.         windowContent.removeEventListener("load", UserAgentSwitcherUpgrade.openUpgradePage, false);
  168.  
  169.         window.setTimeout(function() { windowContent.selectedTab = windowContent.addTab("http://chrispederick.com/work/user-agent-switcher/installed/" + UserAgentSwitcherUpgrade.getVersion().replace(/\./g, "") + "/"); }, 0);    
  170.     },
  171.  
  172.     // Parses the version number
  173.     parseVersion: function(version)
  174.     {
  175.         // If the version is set
  176.         if(version)
  177.         {
  178.             var splitVersion       = version.split(".");
  179.             var parsedVersion      = splitVersion[0] + ".";
  180.             var splitVersionLength = splitVersion.length;
  181.     
  182.             // Loop through the remaining parts of the version
  183.             for(var i = 1; i < splitVersionLength; i++)
  184.             {
  185.                 parsedVersion += splitVersion[i];
  186.             }
  187.     
  188.             return parseFloat(parsedVersion);
  189.         }
  190.         else
  191.         {
  192.             return 0;
  193.         }
  194.     },
  195.     
  196.     // Sets the version
  197.     setVersion: function()
  198.     {
  199.         UserAgentSwitcherPreferences.setStringPreference("useragentswitcher.version", UserAgentSwitcherUpgrade.getVersion());
  200.     },
  201.     
  202.     // Upgrades the extension
  203.     upgrade: function()
  204.     {
  205.         var previousVersion = this.parseVersion(UserAgentSwitcherPreferences.getStringPreference("useragentswitcher.version", true));
  206.         var version         = this.parseVersion(this.getVersion());
  207.  
  208.         // If the versions do not match
  209.         if(previousVersion != version)
  210.         {
  211.             // If the previous version is less than 0.7
  212.             if(previousVersion < this.parseVersion("0.7"))
  213.             {
  214.                 this.migrateTo07();
  215.             }
  216.  
  217.             this.installUserAgents();
  218.  
  219.             window.getBrowser().addEventListener("load", UserAgentSwitcherUpgrade.openUpgradePage, false);
  220.     
  221.             UserAgentSwitcherPreferences.setStringPreference("useragentswitcher.version", version);
  222.         }
  223.     }
  224. };
  225.